/*
 * @Description: 轨迹线
 * @Date: 2022-04-15 11:50:32
 * @Author: yeming
 */
import { fromLonLat } from 'ol/proj.js'
import { Line, Line3d } from './line.js'
import { removeFeatureAndDeleteHashMap, removeAllFeatureAndDeleteHashMap } from '../../common/removeFeature.js'
import OlFeature from 'ol/Feature'
import OlGeomLineString from 'ol/geom/LineString'
import { setOlStyleSolidLine, setOlStyleDashLine } from '../../../common/olStyle.js'

const DYNAMIC_TRACK_LINE_LENGTH = 1024 // java过来的动态航迹总长度

// 默认轨迹线颜色（蓝色）
const DEFAULT_LINE = { 'fillColor': '#00F', 'strokeColor': 'rgba(0, 153, 255, 1)', 'strokeWidth': 2, 'dash': [10, 6] }

class TrackLine {
  constructor(ol2d, ol3d, hashMap, vectorSource) {
    this.ol2d = ol2d
    this.ol3d = ol3d
    this.entities = ol3d.entities

    this.hashMap = hashMap
    this.vectorSource = vectorSource // uav相关矢量图层数据源
  }

  /**
 * @desc: 添加轨迹线（初始化）
 * @param {Number} sessionId
 * @param {Array} latlng [lon, lat, h]
 */
  addTrackLine (sessionId, latlng, style) {
    // 2d
    let line = new Line(this.ol2d, this.ol3d, this.vectorSource)
    line.addTrackLine(sessionId, latlng, style)
    // 3d
    let positions = []
    positions[0] = new Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1], latlng[2])
    positions[1] = new Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1], latlng[2])
    let line3d = new Line3d(this.entities, 'trackLine_' + sessionId, null, false, false)
    line3d.positions = positions

    this.hashMap.set(sessionId, { 'item2d': line, 'item3d': line3d })
  }

  /**
   * @desc: 更新轨迹线（实时轨迹，每次接收一条数据）
   * @param {Number} sessionId
   * @param {Array} latlng [lon, lat, h]
   */
  updaeTrackLine (sessionId, latlng) {
    let hasTrackLine = this.hashMap.has(sessionId)
    if (!hasTrackLine) this.addTrackLine(sessionId, latlng)
    else {
      // 此处是 LineString 对象
      let coordinate = fromLonLat(latlng)
      let dynamicTrackLine = this.hashMap.get(sessionId).item2d.dynamicTrackLine
      let coordinateArr = dynamicTrackLine.getCoordinates()
      coordinateArr.push(coordinate)
      // 大于 2048 适当清除开头数据
      if (coordinateArr.length > DYNAMIC_TRACK_LINE_LENGTH) coordinateArr.splice(0, 1)
      dynamicTrackLine.setCoordinates(coordinateArr)

      // 3d （线特有的更新方式）
      let dynamicTrackLine3d = this.hashMap.get(sessionId).item3d
      let positions = dynamicTrackLine3d.positions
      if (positions.length > DYNAMIC_TRACK_LINE_LENGTH) positions.splice(0, 1)
      positions.push(new Cesium.Cartesian3.fromDegrees(latlng[0], latlng[1], latlng[2]))

    }
  }

  /**
   * @desc: 删除指定飞机轨迹线
   * @param {Number} sessionId
   */
  delTrackLine (sessionId) {
    removeFeatureAndDeleteHashMap(sessionId, this.hashMap, this.vectorSource, this.entities)
  }

  /**
   * @desc: 删除所有飞机轨迹线
   */
  delAllTrackLines () {
    removeAllFeatureAndDeleteHashMap(this.hashMap, this.vectorSource, this.entities)
  }

  /**
   * @desc: 添加批量回放视频数据
   * @param {Number} sessionId
   * @param {Array} latlng [[lon, lat, h], [lon, lat, h], ......, [lon, lat, h]]
   */
  addPlayBackTrackLine (sessionId, latlng) {
    const feature = new OlFeature({
      id: sessionId,
      sessionId: sessionId,
      type: 'allTrackLine',
      geometry:  new OlGeomLineString(latlng)
    })
    feature.setStyle(setOlStyleSolidLine(DEFAULT_LINE))
    this.feature = feature
    this.vectorSource.addFeature(feature)
  }
}

export default TrackLine
